<?php
/**
 * 会员卡类型列表
 */

namespace app\api\controller\v1;

use app\api\model\MemberType as MemberTypeModel;

class MemberType
{
    /**
     * 获取全部
     */
    public function getDataAll()
    {
        return MemberTypeModel::order('id desc')->select()->toArray();
    }

    /**
     * 获取列表
     * @validate('PagingParameter')
     * @auth('查看活动商家','活动商家管理')
     */
    public function getDataList($page,$limit)
    {
        $where = [];
        $query = MemberTypeModel::where($where)->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function (&$item){
            if($item['type'] == 1) {
                $item['type_name'] = '日卡';
            } elseif($item['type'] == 2) {
                $item['type_name'] = '周卡';
            } elseif($item['type'] == 3) {
                $item['type_name'] = '月卡';
            } elseif($item['type'] == 4) {
                $item['type_name'] = '季卡';
            } elseif($item['type'] == 5) {
                $item['type_name'] = '年卡';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return MemberTypeModel::where('id',$id)->find();
    }

    /**
     * 修改信息
     * @validate('CheckMemberType')
     */
    public function createData($name,$money,$type,$look_num)
    {
        MemberTypeModel::create([
            'name' => $name,
            'money' => $money,
            'type' => $type,
            'look_num' => $look_num,
        ]);
        return writeJson(202, [], '添加成功！');
    }

    /**
     * 修改信息
     * @validate('CheckMemberType')
     */
    public function updateData($id,$name,$money,$type,$look_num)
    {
            $info = MemberTypeModel::get($id);
            if ($info) {
                $info->name = $name;
                $info->money = $money;
                $info->type = $type;
                $info->look_num = $look_num;
                $info->save();
            }
            return writeJson(202, [], '修改成功！');
    }

    /**
     * @validate('IdMustBePositiveInt')
     */
    public function deleteData($id)
    {
        MemberTypeModel::destroy($id);
        return writeJson(203, [], '删除成功！');
    }
}